<template>
  <div class="home-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <h1 class="page-title">
        <el-icon class="title-icon">
          <Shop />
        </el-icon>
        供应链总管
      </h1>
      <div class="header-right">
        <span class="welcome-text">欢迎，{{userInfo.name}}！</span>
      </div>
    </div>

    <!-- 统计卡片区域 -->
    <div class="stat-cards">
      <el-card class="stat-card">
        <div class="stat-card__content">
          <div class="stat-card__title">今日处理通知</div>
          <div class="stat-card__number">13</div>
          <div class="stat-card__unit">条</div>
        </div>
      </el-card>

      <el-card class="stat-card">
        <div class="stat-card__content">
          <div class="stat-card__title">原材料库存预警</div>
          <div class="stat-card__number">8</div>
          <div class="stat-card__unit">种</div>
        </div>
      </el-card>

      <el-card class="stat-card">
        <div class="stat-card__content">
          <div class="stat-card__title">待确认订单</div>
          <div class="stat-card__number">8</div>
          <div class="stat-card__unit">单</div>
        </div>
      </el-card>
    </div>

    <!-- 快捷导航卡片 -->
    <div class="navigation-cards">
      <el-card class="nav-card" shadow="hover" @click="navigateToOrders">
        <div class="nav-card__content">
          <div class="nav-card__icon order-icon">
            <el-icon>
              <Tickets />
            </el-icon>
          </div>
          <div class="nav-card__info">
            <div class="nav-card__title">供应链订单</div>
            <div class="nav-card__desc">管理原材料订单，跟踪订单状态</div>
            <div class="nav-card__action">
              <span>立即查看</span>
              <el-icon>
                <ArrowRight />
              </el-icon>
            </div>
          </div>
        </div>
      </el-card>

      <el-card class="nav-card" shadow="hover" @click="navigateToQuality">
        <div class="nav-card__content">
          <div class="nav-card__icon quality-icon">
            <el-icon>
              <Shop />
            </el-icon>
          </div>
          <div class="nav-card__info">
            <div class="nav-card__title">供应商质量看板</div>
            <div class="nav-card__desc">监控供应商质量，管理供应商信息</div>
            <div class="nav-card__action">
              <span>立即查看</span>
              <el-icon>
                <ArrowRight />
              </el-icon>
            </div>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 提示信息 -->
    <div class="alert-box">
      <div class="alert-content">
        请及时处理质量反馈通知，确保供应链畅通及时。
      </div>
    </div>

    <!-- 统计卡片区域 -->
    <div class="stat-cards">
      <el-card class="stat-card">
        <div class="stat-card__content">
          <div class="stat-card__title">供应商总数</div>
          <div class="stat-card__number">10</div>
        </div>
      </el-card>

      <el-card class="stat-card">
        <div class="stat-card__content">
          <div class="stat-card__title">整体合格率</div>
          <div class="stat-card__number">98.6%</div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { Tickets, Shop, ArrowRight } from '@element-plus/icons-vue'
import { userInfoStore } from '@/stores/userInfo';
import { getUserInfoService } from '@/api/UsersApi.ts';
import { ElMessage } from 'element-plus';

const userInfo = userInfoStore();
const router = useRouter()

const searchKeyword = ref('')
const currentPage = ref(1)
const totalItems = ref(9)

// 模拟表格数据
const tableData = ref([
  {
    materialCode: 'BATCH-0014',
    batchNumber: 'BZ102',
    supplier: '中芯国际',
    qualityStatus: '合格',
    receiveTime: 'XXXX-XX-XX',
    processStatus: '待发送'
  },
  {
    materialCode: 'BATCH-0014',
    batchNumber: 'BZ102',
    supplier: '长江存储',
    qualityStatus: '合格',
    receiveTime: 'XXXX-XX-XX',
    processStatus: '待发送'
  }
])

// 获取状态对应的类型
const getStatusType = (status: string) => {
  switch (status) {
    case '合格':
      return 'success'
    case '不合格':
      return 'danger'
    case '待检':
      return 'warning'
    default:
      return 'info'
  }
}

// 获取处理状态对应的类型
const getProcessStatusType = (status: string) => {
  switch (status) {
    case '已发送':
      return 'success'
    case '待发送':
      return 'warning'
    case '已拒绝':
      return 'danger'
    default:
      return 'info'
  }
}

// 查看详情
const handleDetail = (row: any) => {
  console.log('查看详情', row)
}

// 审核通过
const handleApprove = (row: any) => {
  console.log('审核通过', row)
}

// 分页处理
const handlePageChange = (page: number) => {
  currentPage.value = page
  // 这里应该调用API获取对应页的数据
  console.log('当前页:', page)
}

// 导航到订单页面
const navigateToOrders = () => {
  router.push('/supervisor/order')
}

// 导航到供应商质量看板
const navigateToQuality = () => {
  router.push('/supervisor/supplier-quality')
}

const getUserInfo = async () => {
  try {
    let result = await getUserInfoService();
    //将用户信息保存到仓库中
    userInfo.setUserInfo(result.data);
  } catch (error) {
    ElMessage.error('获取用户信息失败');
  }
}

//当打开页面时获取当前用户基本信息
getUserInfo();
</script>

<style lang="scss" scoped>
.home-container {
  padding: 20px;
  background: #f5f7fa;
  min-height: 100vh;

  .page-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 30px;
      padding: 20px;
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      border-radius: 12px;
      box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);

      .page-title {
        font-size: 28px;
        font-weight: bold;
        color: #fff;
        margin: 0;
        display: flex;
        align-items: center;
        gap: 12px;

        .title-icon {
          margin-right: 12px;
          font-size: 32px;
          color: white;
        }
      }

      .header-right {
        display: flex;
        align-items: center;
        gap: 16px;

        .welcome-text {
          color: white;
          font-size: 16px;
          font-weight: 500;
        }
      }

      .action-btn {
        background: rgba(255, 255, 255, 0.2);
        border: 1px solid rgba(255, 255, 255, 0.3);
        color: white;
        border-radius: 8px;
        padding: 12px 24px;
        font-weight: 500;
        backdrop-filter: blur(10px);

        &:hover {
          background: rgba(255, 255, 255, 0.3);
        }

        .el-icon {
          margin-right: 8px;
        }
      }
  }

  .stat-cards {
    display: flex;
    gap: 20px;
    margin-bottom: 20px;

    .stat-card {
      flex: 1;
      background: white;
      border-radius: 24px;
      padding: 24px;
      box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
      transition: all 0.3s ease;
      position: relative;
      overflow: hidden;

      &__content {
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 20px 0;
      }

      &__title {
        font-size: 16px;
        color: #666;
        margin-bottom: 8px;
      }

      &__number {
        font-size: 28px;
        font-weight: bold;
        color: #409eff;
        margin-bottom: 4px;
      }

      &__unit {
        font-size: 14px;
        color: #999;
      }
    }
  }

  .navigation-cards {
    
    display: flex;
    gap: 20px;
    margin-bottom: 20px;

    .nav-card {
      border-radius: 24px;
      flex: 1;
      cursor: pointer;
      transition: all 0.3s ease;

      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
      }

      &__content {
        display: flex;
        align-items: center;
        padding: 24px;
      }

      &__icon {
        width: 60px;
        height: 60px;
        border-radius: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 20px;

        .el-icon {
          font-size: 28px;
          color: white;
        }

        &.order-icon {
          background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }

        &.quality-icon {
          background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
        }
      }

      &__info {
        flex: 1;
      }

      &__title {
        font-size: 18px;
        font-weight: bold;
        color: #333;
        margin-bottom: 8px;
      }

      &__desc {
        font-size: 14px;
        color: #666;
        margin-bottom: 12px;
        line-height: 1.4;
      }

      &__action {
        display: flex;
        align-items: center;
        color: #409eff;
        font-size: 14px;
        font-weight: 500;

        .el-icon {
          margin-left: 4px;
          font-size: 12px;
        }
      }
    }
  }

  .old-stat-cards {
    display: flex;
    gap: 20px;
    margin-bottom: 20px;

    .stat-card {
      flex: 1;

      &__content {
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 20px 0;
      }

      &__title {
        font-size: 16px;
        color: #606266;
        margin-bottom: 10px;
      }

      &__number {
        font-size: 36px;
        font-weight: bold;
        color: #303133;
      }

      &__unit {
        font-size: 14px;
        color: #909399;
        margin-top: 5px;
      }
    }
  }

  .data-card {
    margin-bottom: 20px;

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .header-right {
        display: flex;
        gap: 10px;
      }

      .search-input {
        width: 250px;
      }
    }

    .table-footer {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-top: 20px;
    }
  }

  .alert-box {
    background-color: #fffbe6;
    padding: 10px 20px;
    border-radius: 4px;
    margin-bottom: 20px;

    .alert-content {
      color: #e6a23c;
      font-size: 14px;
    }
  }
}
</style>